// pages/shop/shop.js
Page({
  data: {
    shopNum: 0,
    headerStyle: "",
    swiperList: [],
    shopTypeList: [],
    shopModelList: []
  },
  onLoad(options) {
    this.getCenterPosition()
    wx.cloud.callFunction({
      name: "getSwiper",
    }).then(ress => {
      this.setData({
        swiperList: ress.result.data
      })
    })
    wx.cloud.callFunction({
      name: "getShopType"
    }).then(res => {
      // console.log(res)
      this.setData({
        shopTypeList: res.result.data
      })
    })
    wx.cloud.callFunction({
      name: "getShopInfo"
    }).then(res => {
      // console.log(res)
      this.setData({
        shopModelList: res.result.data
      })
    })
  },
  getCenterPosition() {
    // 获取胶囊的样式内容
    let {
      top,
      height
    } = wx.getMenuButtonBoundingClientRect();
    // console.log(wx.getMenuButtonBoundingClientRect())
    let centerPosition = top + height / 2;
    let headerStyle = "margin-top:calc(" + centerPosition + "px - 30rpx";
    this.setData({
      headerStyle: headerStyle
    })
  },
  // 跳转到对应的商品类型
  typeClick(event) {
    // console.log(event)
    let {
      id
    } = event.currentTarget.dataset
    let nav = this.data.shopTypeList[id].title
    let typeId = id
    wx.setStorage({
      key: "typeRecord",
      data: {
        typeId,
        nav
      }
    })
    wx.navigateTo({
      url: '/pages/shopMenu/shopMenu',
    })
  },
  // 增加商品数量
  addShop(event) {
    // console.log(event)
    let {
      cartid
    } = event.currentTarget.dataset
    // console.log(cartid)
    let newCount = this.data.shopModelList[cartid].count
    // console.log(cartid,nowCount)
    let newList = this.data.shopModelList
    newCount++
    newList[cartid].count = newCount
    wx.showToast({
      title: '商品添加成功',
      icon: 'none',
      duration: 2000
    })
    this.setData({
      shopModelList: newList
    })
    // console.log(this.data.shopModelList)
    this.countSum()
  },
  // 统计总商品数量及
  countSum() {
    let shopModel = this.data.shopModelList
    let num = 0
    for (const model of shopModel) {
      // console.log(model.count)
      if (model.count > 0) {
        num += model.count
      }
    }
    this.setData({
      shopNum: num
    })
  },
  // 进入商品详情
  shopDesc(event) {
    let {
      shopid
    } = event.currentTarget.dataset
    console.log(this.data.shopModelList)
    console.log(event)
    let newId = shopid
    wx.setStorage({
      key: "shopInfoRecord",
      data: newId
    })
    wx.navigateTo({
      url: '/pages/shopDetails/shopDetails',
    })
  },
  // 进入购物车
  comeCart() {
    let proList = []
    let shopModel = this.data.shopModelList
    console.log(shopModel)
    for (const model of shopModel) {
      if (model.count > 0) {
        proList.push(model)
      }
    }
    wx.getStorage({
      key: "orderRecord",
      success: res => {
        let orderList = res.data
        orderList.push(proList)
        wx.setStorage({
          key: "orderRecord",
          data: orderList
        })
        wx.navigateTo({
          url: '/pages/shopCart/shopCart',
        })
      },
      fail: res => {
        let orderList = [proList]
        wx.setStorage({
          key: "orderRecord",
          data: orderList
        })
        wx.navigateTo({
          url: '/pages/shopCart/shopCart',
        })
      }
    })
    this.clearData()

  },
  clearData(){
    this.setData({
      shopNum: 0
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})